$(function () {

    //变量抽取
    var car_dataGrid = $("#car_dataGrid");
    var car_dialog = $("#car_dialog");
    var car_form = $("#car_form");

    car_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/car/query",
        fit: true,
        columns: [[
            {field: 'owner', title: '车主姓名', width: 100},
            {field: 'brand', title: '车辆品牌', width: 100},

           /* {field: 'brand', title: '车辆品牌', width: 100,formatter:function (value,row,index) {
                   // console.log(value);
                    var name;
                    $.ajax({
                        type: "POST",
                        url: "/dictionaryItem/selectById",
                        async: false,
                        data: {id: value},
                        success: function (data) {
                            name = data.name;
                        }
                    });
                    return name;
                }},*/

            {field: 'model', title: '车辆型号', width: 100},
            {field: 'number', title: '车牌号码', width: 100},
            {field: 'gas', title: '排气量/L', width: 100},
            {field: 'purchaseDate', title: '购买日期', width: 100},
            {field: 'price', title: '市场估价/万元', sortable: true,width: 100},
            {field: 'type', title: '分类', width: 100,formatter: function(value,row,index){
                  return value == 0? "载物":"载客";
                }
            },
            {field: 'loadSize', title: '车载大小', width: 100, formatter:function (value,row,index) {
                    if(value==0){
                        return "小型";
                    }else if(value==1){
                        return "中型";
                    }else {
                        return "大型";
                    }
                }},
            {field: 'remark', title: '备注信息', width: 100}

        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true,//只允许选择一行
        sortName: 'price',
        sortOrder: 'DESC'
    });

    car_dialog.dialog({
        width: 400,
        height: 400,
        buttons: "#btns",
        closed: true,
        onClose: function () {
            car_form.form("clear");
        },
        resizeable:true
    });

    //方法抽成对象统一管理
    var methodObj = {
        searchForm: function () {
            var keyword = $("#keyword").textbox('getValue');
            car_dataGrid.datagrid('load', {
                keyword: keyword
            });
        },

        add: function () {
            car_dialog.dialog("setTitle", "新增车辆");
            car_dialog.dialog("open");
        },

        edit: function () {
            var row = car_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中编辑数据', 'info');
                return;
            }
            row["brand"]=row.brand;
            //回显数据
            car_form.form("load", row);
            car_dialog.dialog("setTitle", "编辑车辆");
            car_dialog.dialog("open");
        },
        remove: function () {
            var row = car_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中一条数据', 'info');
                return;
            }
            $.messager.confirm('提示', '确认删除该数据吗？', function (r) {
                if (r) {
                    $.get("/car/delete", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('提示', '删除成功', 'info', function () {
                                car_dataGrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('提示', data.msg, 'error')
                        }
                    })
                }
            })
        },
        reload: function () {
            car_dataGrid.datagrid("load",{});
            $("#keyword").textbox("clear");
        },
        save: function () {
            car_form.form('submit', {
                url: "/car/saveOrUpdate",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            methodObj.cancel();
                            car_dataGrid.datagrid("load");
                        })
                    } else {
                        $.messager.alert("提示", "操作失败", "error");
                    }
                }
            })
        },
        cancel: function () {
            car_dialog.dialog("close");
        }
    };

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
});
